//The MIT License
//
//Copyright (c) 2009 nodchip
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
package tv.dyndns.kishibe.client;

import tv.dyndns.kishibe.client.packet.PacketProblemData;
import tv.dyndns.kishibe.client.packet.PacketResult;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class PanelResult extends VerticalPanel implements ClickHandler {
	private Grid grid;
	private VerticalPanel panelTransition = new VerticalPanel();
	private SimplePanel panelPlayer = new SimplePanel();
	private SimplePanel panelProblem = new SimplePanel();
	private final Button buttonTopPage = new Button("トップページに戻る(現画面のまま)", this);

	public PanelResult(PacketProblemData[] problems) {
		setWidth("800px");
		setHorizontalAlignment(ALIGN_CENTER);
		setVerticalAlignment(ALIGN_MIDDLE);

		Label title = new Label("成績発表");
		add(title);
		setCellHeight(title, "50px");
		setCellWidth(title, "800px");

		{
			add(panelTransition);
			panelTransition.setVisible(false);
			panelTransition.setHorizontalAlignment(ALIGN_CENTER);

			HTML html = new HTML("<a href=\"./QMAClone.html\">トップページに戻る(ページ再読み込み)</a>");
			panelTransition.add(html);
			panelTransition.setCellHeight(html, "50px");
			panelTransition.setCellWidth(html, "800px");

			panelTransition.add(buttonTopPage);
			panelTransition.setCellHeight(buttonTopPage, "50px");
			panelTransition.setCellWidth(buttonTopPage, "800px");
		}

		add(panelPlayer);

		// 問題
		panelProblem.setWidget(new WidgetProblemReport(problems, true, false, Constant.MAX_PROBLEMS_PER_SESSION));

		add(panelProblem);

		SharedData.getInstance().setIsPlaying(false);
	}

	public void setPlayerList(PacketResult[] result) {
		panelTransition.setVisible(true);

		grid = new Grid(result.length + 1, 4);
		grid.addStyleName("gridFrame");
		grid.addStyleName("gridFontNormal");
		grid.setText(0, 1, "プレイヤー名");
		grid.setText(0, 2, "得点");
		grid.setText(0, 3, "順位");

		for (int i = 0; i < result.length; ++i) {
			final Image image = new Image(Constant.ICON_URL_PREFIX + result[i].imageFileName);
			image.setPixelSize(Constant.ICON_SIZE, Constant.ICON_SIZE);
			grid.setWidget(i + 1, 0, image);

			String name = result[i].name;
			if (name.indexOf(Constant.DELEMITER_PLAYER_NAME) != -1) {
				name = name.substring(0, name.lastIndexOf(Constant.DELEMITER_PLAYER_NAME));
			}

			grid.setText(i + 1, 1, name);
			grid.setText(i + 1, 2, result[i].score + "点");
			grid.setText(i + 1, 3, result[i].rank + "位");
		}

		panelPlayer.add(grid);
	}

	@Override
	public void onClick(ClickEvent event) {
		final Object sender = event.getSource();
		if (sender == buttonTopPage) {
			Controller.getInstance().setScene(new SceneLobby());
		}
	}
}
